import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:mobile_app.core/core/domain/config/config.dart';
import 'package:mobile_app.core/core/domain/entities/purchase_offer_source.dart';
import 'package:mobile_app.core/google_apple_store_purchase_flow_display/application/bloc.dart';
import 'package:mobile_app.core/google_apple_store_purchases/application/bloc/bloc.dart';
import 'package:mobile_app.core/google_apple_store_purchases/application/models/purchase_statuses.dart';
import 'package:mobile_app.core/google_apple_store_purchases/domain/entities/lifetime_purchase_item_model.dart';
import 'package:mobile_app.core/google_apple_store_purchases/domain/entities/month_subsciption_model.dart';
import 'package:mobile_app.core/google_apple_store_purchases/domain/entities/year_subsciption_model.dart';
import 'package:mobile_app.core/url_launcher/application/bloc/bloc.dart';
import 'package:mobile_app.core/user_info/application/bloc/bloc.dart';
import 'package:mobile_app.home.core/google_apple_store_purchase_page/domain/initialize/use_case.dart';

part 'bloc_state.dart';

class GoogleAppleStorePurchasePageBloc extends Cubit<GoogleAppleStorePurchasePageBlocState> {
  GoogleAppleStorePurchasePageBloc({
    required GoogleAppleStorePurchaseBloc googleAppleStorePurchaseBloc,
    required GoogleAppleStorePurchaseFlowDisplayBloc googleAppleStorePurchaseFlowDisplayBloc,
    required UserInfoBloc userInfoBloc,
    required UrlLauncherBloc urlLauncherBloc,
    required Initialize initialize,
  })  : _googleAppleStorePurchaseFlowDisplayBloc = googleAppleStorePurchaseFlowDisplayBloc,
        _urlLauncherBloc = urlLauncherBloc,
        super(_getInitialState(
          googleAppleStorePurchaseBlocState: googleAppleStorePurchaseBloc.state!,
          userInfoBlocState: userInfoBloc.state!,
        )) {
    initialize();

    _googleAppleStorePurchaseBlocSubscription =
        googleAppleStorePurchaseBloc.stream.listen(_onGoogleAppleStorePurchaseBlocStateChanged);

    _userInfoBlocSubscription = userInfoBloc.stream.listen(_onUserInfoBlocStateChanged);
  }

  final GoogleAppleStorePurchaseFlowDisplayBloc _googleAppleStorePurchaseFlowDisplayBloc;
  final UrlLauncherBloc _urlLauncherBloc;

  late final StreamSubscription<GoogleAppleStorePurchaseBlocState>
      _googleAppleStorePurchaseBlocSubscription;
  late final StreamSubscription<UserInfoBlocState> _userInfoBlocSubscription;

  static GoogleAppleStorePurchasePageBlocState _getInitialState({
    required GoogleAppleStorePurchaseBlocState googleAppleStorePurchaseBlocState,
    required UserInfoBlocState userInfoBlocState,
  }) {
    return GoogleAppleStorePurchasePageBlocState(
      monthSubscriptionModel: googleAppleStorePurchaseBlocState.monthSubscriptionModel!,
      yearSubscriptionModel: googleAppleStorePurchaseBlocState.yearSubscriptionModel!,
      lifetimePurchaseItemModel: googleAppleStorePurchaseBlocState.lifetimePurchaseItemModel!,
      selectedPurchaseItemSku: googleAppleStorePurchaseBlocState.yearSubscriptionModel!.sku,
      purchaseRequestInProgress:
          googleAppleStorePurchaseBlocState.purchaseStatus is PurchaseInProgress,
      premiumAccessActive: userInfoBlocState.premiumAccessStatus.hasFullAccess,
    );
  }

  void _onGoogleAppleStorePurchaseBlocStateChanged(
      GoogleAppleStorePurchaseBlocState googleAppleStorePurchaseBlocState) {
    emit(state.copyWith(
      purchaseRequestInProgress: () =>
          googleAppleStorePurchaseBlocState.purchaseStatus is PurchaseInProgress,
    ));
  }

  void _onUserInfoBlocStateChanged(UserInfoBlocState userInfoBlocState) {
    emit(state.copyWith(
      premiumAccessActive: () => userInfoBlocState.premiumAccessStatus.hasFullAccess,
    ));
  }

  void selectPurchaseItem({
    required String purchaseItemSku,
  }) {
    emit(state.copyWith(
      selectedPurchaseItemSku: () => purchaseItemSku,
    ));
  }

  void requestPurchase() {
    _googleAppleStorePurchaseFlowDisplayBloc.requestPurchase(
      purchaseItemSku: state.selectedPurchaseItemSku,
      purchaseOfferSource: PurchaseOfferSource.purchasePage,
    );
  }

  void openPrivacyPolicy() {
    _urlLauncherBloc.launch(
      browserUrl: Config().privacyPolicyUrl,
    );
  }

  void openTermsOfUse() {
    _urlLauncherBloc.launch(
      browserUrl: Config().termsOfUseUrl,
    );
  }

  @override
  Future<void> close() {
    _googleAppleStorePurchaseBlocSubscription.cancel();
    _userInfoBlocSubscription.cancel();
    return super.close();
  }
}
